package com.sinosoft.hydra.um.domain.permission;

import cn.com.sinux.spring.commons.TreeNode;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * created:2014/12/03
 * 权限资源
 *
 * @author oujianlin@sinosoft.co
 */
@Entity
@DynamicInsert(true)
@DynamicUpdate(true)
@Table(name = "UM_SYS_RESOURCE")
public class SysResource extends IdEntity implements TreeNode<SysResource> {

    private static final long serialVersionUID = 4595450080126650330L;
    private String resourceName;
    private String resourceType;
    private String resourceUrl;
    private SysResource sysResource;
    private String permission;
    private Integer available;
    private String parentId;
    private Integer orderNo;
    private String icon;
    private List<SysRole> sysRoles = new ArrayList<SysRole>();

    private List<SysResource> children = new ArrayList<SysResource>();

    public SysResource() {
    }

    @Column(name = "ORDER_NO")
    public Integer getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(Integer orderNo) {
        this.orderNo = orderNo;
    }

    @Column(name = "ICON")
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    /**
     * 父级资源ID
     *
     * @return String
     */
    @Column(name = "PARENT_ID")
    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    /**
     * 父级资源对象
     *
     * @return SysResource
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID", nullable = false, updatable = false, insertable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    public SysResource getSysResource() {
        return sysResource;
    }

    public void setSysResource(SysResource sysResource) {
        this.sysResource = sysResource;
    }


    public SysResource(String id) {
        this.id = id;
    }

    /**
     * 资源名字
     *
     * @return String
     */
    @Column(name = "RESOURCE_NAME")
    public String getResourceName() {
        return this.resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    /**
     * 资源类型(menu || button)
     *
     * @return String
     */
    @Column(name = "RESOURCE_TYPE")
    public String getResourceType() {
        return this.resourceType;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    /**
     * 资源路径
     *
     * @return String
     */
    @Column(name = "RESOURCE_URL")
    public String getResourceUrl() {
        return this.resourceUrl;
    }

    public void setResourceUrl(String resourceUrl) {
        this.resourceUrl = resourceUrl;
    }

    /**
     * 权限码
     *
     * @return String
     */
    @Column(name = "PERMISSION")
    public String getPermission() {
        return this.permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    /**
     * 是否可用【0-不可用;1-可用】
     *
     * @return Integer
     */
    @Column(name = "AVAILABLE")
    public Integer getAvailable() {
        return available;
    }

    public void setAvailable(Integer available) {
        this.available = available;
    }

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY, mappedBy = "sysResources")
    public List<SysRole> getSysRoles() {
        return sysRoles;
    }

    public void setSysRoles(List<SysRole> sysRoles) {
        this.sysRoles = sysRoles;
    }


    @Transient
    public List<SysResource> getChildren() {
        return children;
    }

    public void setChildren(List<SysResource> children) {
        this.children = children;
    }

    public void addChild(SysResource child) {
        this.children.add(child);
    }

}
